<?php
// $Id: FieldsetInput.php 27 2010-11-16 12:49:54Z mjohnson60@gmail.com $


/**
 * Enter description here ...
 * 
 * @author Mark Phillip Johnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class FieldsetInput extends FormElement
{
	/**
	 * @var boolean Enter description here ...
	 */
	var $collapsible = FALSE;

	/**
	 * __construct()
	 * 
	 * Enter description here ...
	 * 
	 * @param unknown_type $name
	 * @param unknown_type $value
	 */
	public function __construct($name, $value)
	{
		$this->type = 'fieldset';
		
		if (is_array($value))
		{
			if (!empty($value['#collapsible']))
			{
				$this->collapsible = $value['#collapsible'];
			}
			
			parent::__construct($value, $name);
		}
		else
		{
			parent::__construct(NULL, $name, $value, NULL, NULL, NULL);
		}
	}

	/**
	 * _compile()
	 * 
	 * Enter description here ...
	 * 
	 * @return string
	 */
	public function _compile()
	{
		return $this->render();
	}

	/* 
	 * @see FormElement::render()
	 */
	public function render()
	{
		if (!empty($this->collapsible))
		{
			Athena::addScripts('misc/collapse.js');
			
			if (!isset($this->attributes['class']))
			{
				$this->attributes['class'] = '';
			}
			
			$this->attributes['class'] .= ' collapsible';
			if (!empty($this->collapsed))
			{
				$this->attributes['class'] .= ' collapsed';
			}
		}
		
		return '<fieldset' . attributes($this->attributes) . '>' . ($this->title ? '<legend>' . $this->title . '</legend>' : '') . (isset($this->description) && $this->description ? '<div class="description">' . $this->description . '</div>' : '') . (!empty($this->content) ? $this->content : '') . (isset($this->value) ? $this->value : '') . "</fieldset>\n";
	}
}
